<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\LinkModel;
use app\admin\model\LinkCategoryModel;

class LinkController extends AdminBaseController
{
    protected $targets = ["_blank" => "新标签页打开", "_self" => "本窗口打开"];

    /**
     * 友情链接管理
     * @adminMenu(
     *     'name'   => '友情链接',
     *     'parent' => 'admin/Link/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 50,
     *     'icon'   => '',
     *     'remark' => '友情链接管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $param               = $this->request->param();
        $categoryId          = $this->request->param('category', 0, 'intval');
        $param['sort_by']    = $this->request->param('sort_by','','trim');
        $param['list_order'] = $this->request->param('list_order','DESC','trim');

        $linkModel = new LinkModel();
        $data      = $linkModel->adminList($param);
        $data->appends($param);

        //获取列表项
        $listItem = get_list_item_column('link');
        $this->assign('list_item', $listItem);
        
        //分类列表
        $where = ['site_id' => get_site_id()];
        $categorys   = db('LinkCategory')->where($where)->column('cat_name','cat_id');
        $catOptions = html_options($categorys, $categoryId);
        $this->assign('catOptions', $catOptions);

        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('type', isset($param['type']) ? $param['type'] : 0);
        $this->assign('category', $categoryId);
        $this->assign('links', $data->items());
        $this->assign('page', $data->render());

        $actions = ['add', 'edit', 'delete', 'listOrder', 'toggle'];
        $this->assign('actions', actions_check($this->request->module().'/'.$this->request->controller().'/', $actions));

        return $this->fetch();
    }

    /**
     * 添加友情链接
     * @adminMenu(
     *     'name'   => '添加友情链接',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加友情链接',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $categoryId = $this->request->param('category', 0, 'intval');
        $type       = $this->request->param('type', 1, 'intval');
        $categorys  = db('LinkCategory')->column('cat_name','cat_id');
        $catOptions = html_options($categorys,$categoryId);

        $this->assign('type', $type);
        $this->assign('catOptions', $catOptions);
        $this->assign('category', $categoryId);

        $this->assign('targets', $this->targets);

        return $this->fetch();
    }

    /**
     * 添加友情链接提交保存
     * @adminMenu(
     *     'name'   => '添加友情链接提交保存',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加友情链接提交保存',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        $data      = $this->request->param();
        $linkModel = new LinkModel();

        // 站点ID
        $data['site_id'] = get_site_id();

        $result          = $linkModel->validate(true)->allowField(true)->save($data);
        if ($result === false) {
            $this->error($linkModel->getError());
        }
        
        //操作日志
        admin_log($data['name'], 'add', 'friendly_link');

        $this->success("添加成功！", url("link/index"));
    }

    /**
     * 编辑友情链接
     * @adminMenu(
     *     'name'   => '编辑友情链接',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑友情链接',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id        = $this->request->param('id', 0, 'intval');
        $linkModel = LinkModel::get($id);
        $this->assign('targets', $this->targets);
        $this->assign('link', $linkModel);

        $category =  LinkCategoryModel::get($linkModel['cat_id']);

        $categorys  = db('LinkCategory')->column('cat_name','cat_id');
        $catOptions = html_options($categorys, $linkModel['cat_id']);
        $this->assign('catOptions', $catOptions);
        $this->assign('category', $category);

        return $this->fetch();
    }

    /**
     * 编辑友情链接提交保存
     * @adminMenu(
     *     'name'   => '编辑友情链接提交保存',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑友情链接提交保存',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $data      = $this->request->param();

        $data['status'] = isset($data['status']) ? 0 : 1;

        $linkModel = new LinkModel();
        $result    = $linkModel->validate(true)->allowField(true)->isUpdate(true)->save($data);
        if ($result === false) {
            $this->error($linkModel->getError());
        }  

        //操作日志
        admin_log($data['name'], 'edit', 'friendly_link');

        $this->success("保存成功！", url("link/index"));
    }

    /**
     * 删除友情链接
     * @adminMenu(
     *     'name'   => '删除友情链接',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除友情链接',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $param           = $this->request->param();

        if (isset($param['id'])) {
            $id = $this->request->param('id', 0, 'intval');
            $linkModel = LinkModel::get($id);
            LinkModel::destroy($id);

            //操作日志
            admin_log($linkModel['name'], 'delete', 'friendly_link');
        }

        if (isset($param['ids'])) {
            $linkModel = new LinkModel();
            $ids     = $this->request->param('ids/a');
            $recycle = $linkModel->where(['id' => ['in', $ids]])->delete();

            //操作日志
            admin_log('', 'batch_delete', 'friendly_link');
        }

        $this->success("删除成功！", url("link/index"));
    }

    /**
     * 友情链接排序
     * @adminMenu(
     *     'name'   => '友情链接排序',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '友情链接排序',
     *     'param'  => ''
     * )
     */
    public function listOrder()
    {
        $linkModel = new  LinkModel();
        parent::listOrders($linkModel);

        //操作日志
        admin_log('', 'UPDATE_FRIENDLY_LINK_LISTORDER');

        $this->success("排序更新成功！");
    }

    /**
     * 友情链接显示隐藏
     * @adminMenu(
     *     'name'   => '友情链接显示隐藏',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '友情链接显示隐藏',
     *     'param'  => ''
     * )
     */
    public function toggle()
    {
        $data      = $this->request->param();
        $linkModel = new LinkModel();

        if (isset($data['ids']) && !empty($data["display"])) {
            $ids = $this->request->param('ids/a');
            $linkModel->where(['id' => ['in', $ids]])->update(['status' => 1]);
        }

        if (isset($data['ids']) && !empty($data["hide"])) {
            $ids = $this->request->param('ids/a');
            $linkModel->where(['id' => ['in', $ids]])->update(['status' => 0]);
        }

        $this->success("操作成功！");
    }

}
